﻿/*!
* jQuery tree plugin: 
* 
* version 0.1 2011-03-10
* Requires jQuery v1.4.2 or later
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
* Authors: Kim Seoung Min
* Company : Kongyoung DB
*/

(function ($) {
    $.fn.SuperAutoComplete = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.tooltip');
        }
    };

    var AutocompleteisRun = false;

    var methods = {
        init: function (url) {
            var SuperAuto = $(this);
            toCODEID = $(this).after("<input class='txtCODEID input_hiden' id='" + $(this).attr('id') + "_CODEID'  type='text'  />");

            SuperAuto.focusout(function (e) {
            });


            SuperAuto.keydown(function (e) {
                var SuperAuto = $(this);
                if ($("#AutoCompleteUl").length == 0) return;
                if (e.keyCode == '13') {
                    $(this).val($("#AutoCompleteUl").data('select').html());
                    $("#" + $(this).attr('id') + "_CODEID").val($("#AutoCompleteUl").data('select').attr("data"));
                    $(document.body).find('#AutoCompleteDiv').delay(500).remove();
                }
                if (e.keyCode == '38') {
                    var i = $("#AutoCompleteUl").data('index');
                    var _li = $("#AutoCompleteUl").data('select');
                    if (i == 0) {
                        _li.removeClass('ui-state-hover');
                        $("#AutoCompleteUl li").last().addClass('ui-state-hover');
                        $("#AutoCompleteUl").data("index", $("#AutoCompleteUl li").index($("#AutoCompleteUl li").last()));
                        $("#AutoCompleteUl").data('select', $("#AutoCompleteUl li").last());
                    }
                    else {
                        _li.removeClass('ui-state-hover');
                        _li.prev().addClass('ui-state-hover');
                        $("#AutoCompleteUl").data("index", $("#AutoCompleteUl li").index(_li.prev()));
                        $("#AutoCompleteUl").data("select", _li.prev());

                    }
                    SuperAuto.val($("#AutoCompleteUl").data('select').html());
                }
                if (e.keyCode == '40') {
                    var i = $("#AutoCompleteUl").data('index');
                    var _li = $("#AutoCompleteUl").data('select');

                    if (i == $("#AutoCompleteUl li").index($("#AutoCompleteUl li").last())) // 현재가 마지막자료라면
                    {
                        _li.removeClass('ui-state-hover');
                        $("#AutoCompleteUl li").first().addClass('ui-state-hover');
                        $("#AutoCompleteUl").data("index", $("#AutoCompleteUl li").index($("#AutoCompleteUl li").first()));
                        $("#AutoCompleteUl").data('select', $("#AutoCompleteUl li").first());
                    } else {
                        _li.removeClass('ui-state-hover');
                        _li.next().addClass('ui-state-hover');
                        $("#AutoCompleteUl").data("index", $("#AutoCompleteUl li").index(_li.next()));
                        $("#AutoCompleteUl").data("select", _li.next());
                    }
                    SuperAuto.val($("#AutoCompleteUl").data('select').html());
                }
                if (e.keyCode == '27') {
                    $(document.body).find('#AutoCompleteDiv').remove();
                    $("#" + $(this).attr('id') + "_CODEID").val("");
                    $(this).val("");
                }

            });



            SuperAuto.keyup(function (e) {

                if (e.keyCode == '38') { return; }
                if (e.keyCode == '40') { return; }
                if (e.keyCode == '13') { return; }

                if ($(this).val().length < 2) return 0;


                $(document.body).find('#AutoCompleteDiv').remove();
                var SuperAuto = $(this);
                var pl = new JSONClientParameters();
                pl.add("QQ", $(this).val());
                PostJsonData(url, pl, function (data) {
                    var _data = eval("data.Records");
                    var Rows = _data.length;
                    $("#AutoCompleteUl").html("");
                    if (Rows == 0) { return; }

                    $(document.body).append("<div id='AutoCompleteDiv' class='ui-widget-content'><ul id='AutoCompleteUl' ></ul></div>");
                    $("#AutoCompleteDiv").css("top", SuperAuto.offset().top + SuperAuto.outerHeight());
                    $("#AutoCompleteDiv").css("left", SuperAuto.offset().left);

                    for (var i = 0; i < Rows; i++) {
                        var DataRow = _data[i];
                        var datali = "<li class='AutoSerchItem' data='" + DataRow["id"] + "'>" + DataRow["text"] + "</li>";
                        $("#AutoCompleteUl").append(datali);
                    }

                    $("#AutoCompleteUl li").hover(
                    	    function () {
                    	        $(this).addClass('ui-state-hover');
                    	        $("#AutoCompleteUl").data("index", $("#AutoCompleteUl li").index($(this)));
                    	        $("#AutoCompleteUl").data("select", $(this));
                    	        SuperAuto.val($(this).html());
                    	    },
                    	    function () { $(this).removeClass('ui-state-hover'); }
                        );

                    $("#AutoCompleteUl").data('index', 0);
                    $("#AutoCompleteUl").data('select', $("#AutoCompleteUl li").first());

                    $("#AutoCompleteUl li:eq(0)").addClass('ui-state-hover');
                    $("#AutoCompleteUl li").click(function (e) {
                        SuperAuto.val($(this).html());
                        $("#" + SuperAuto.attr('id') + "_CODEID").val($(this).attr("data"));

                        $(document.body).find('#AutoCompleteDiv').remove();
                    })

                });

            });
        },

        WhoIs: function () {
        }
    }
})(jQuery);


//                    alert(_data.length);
//                    var AutoCompleteDiv = $("<div id='#AutoCompleteDiv'><ul id='#AutoCompleteUl' class='ui-widget-content'></ul></div>").appendTo(document.body);
//                    $("#AutoCompleteDiv").css("top", SuperAuto.offset().top + SuperAuto.outerHeight());
//                    $("#AutoCompleteDiv").css("left", SuperAuto.offset().left);

//                    var Rows = _data.length;
//                    if (Rows == 0) { return; }
//                    $("#AutoCompleteUl").html("");

//                    for (var i = 0; i < Rows; i++) {
//                        var DataRow = _data[i];
//                        var datali = "<li class='AutoSerchItem'>" + DataRow["CSTNAME"] + " / " + DataRow["CSTID"] + "</li>";
//                        alert(datali);
//                        $("#AutoCompleteUl").append(datali);
//                    }

//                    $("#AutoCompleteUl li").hover(
//				            function () {
//				                $(this).addClass('ui-state-hover');
//				                // $("#CopyAuto").val($(this).html());
//				                // $("#lm li ul").attr("index", $("#lm li li").index(this));
//				                // $("#SerchAuto").html($("#lm li li").index(this));
//				            },
//				            function () { $(this).removeClass('ui-state-hover'); }
//                        );

//                    $("#AutoCompleteUl li:eq(0)").addClass('ui-state-hover');
//                    $("#AutoCompleteUl li").click(function (e) {
//                        $("#SuperAuto").val($(this).html());
//                        $(document.body).find('#AutoCompleteDiv').remove();
//                    })
//                    AutocompleteisRun = false;
